package com.ym.manager.admin.order;

import com.alibaba.fastjson.JSONObject;
import com.ym.manager.admin.order.model.EnumType;
import com.ym.manager.admin.order.model.Order;
import com.ym.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;


    /**
     * 订单列表
     * @param pageNum
     * @param limit
     * @param order_sn
     * @param time
     * @param merchant_id
     * @return
     */
    public Pages<Order> getList(int pageNum, int limit, String order_sn, int uid, String time, int status, int merchant_id) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("order_sn",order_sn);
        map.put("uid",uid);
        map.put("time",time);
        map.put("status",status);
        map.put("merchant_id",merchant_id);
        int count  = orderDao.getOrderListCount(map);
        Pages<Order> pages = new Pages<>(count,pageNum,limit);
        List<Order> list = orderDao.getOrderList(map);
        list = list.stream().map(x->{
            x.setStatusName(EnumType.ORDER_STATUS.getName(x.getStatus()));
            if(x.source==2){
                x.setOrder_amount(StringHandler.fenToYuan(StringHandler.getString(x.getOrder_amount())));
                x.setGoods_inner_price(StringHandler.fenToYuan(StringHandler.getString(x.getGoods_inner_price())));
            }
            return x;
        }).collect(Collectors.toList());
        pages.setList(list);
        return pages;
    }

    /**
     * 订单详情
     * @param order_sn
     * @return
     */
    public Order detail(String order_sn) {
        Order order = orderDao.detail(order_sn);
        if(StringHandler.isNotEmpty(order)){
            if(order.source==2){
                order.setOrder_amount(StringHandler.fenToYuan(StringHandler.getString(order.getOrder_amount())));
                order.setGoods_inner_price(StringHandler.fenToYuan(StringHandler.getString(order.getGoods_inner_price())));
                if(StringHandler.isNotEmpty(order.getGoods_units())) {
                    Map<String, Object> map = JsonUtil.jsonToBean(order.getGoods_units(), Map.class);
                    order.setGoods_price(StringHandler.getString(map.get("sale_price")));
                }
            }
        }
        return order;
    }

    /**
     * 查询用户信息
     * @param uid
     * @return
     */
    public Map<String, Object> getUserInfo(int uid) {
        return orderDao.getUserInfo(uid);
    }

    /**
     * 变更订单状态
     * @param order_sn 订单编号
     * @param status 状态
     * @return
     */
    public int editOrderStatus(String order_sn, int status) {
        return orderDao.editOrderStatus(order_sn,status,"");
    }
    public int updateOrder(String order_sn, int status,String l_id,String logistics_company,String send_date,String address) {
        return orderDao.updateOrder(order_sn,status,l_id,logistics_company,send_date,address);
    }
    public Pages<Order> back(int pageNum, int limit, String order_sn, int uid, String time) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("order_sn",order_sn);
        map.put("uid",uid);
        map.put("time",time);
        map.put("status", EnumType.ORDER_STATUS.申请退款.getStatus());
        int count  = orderDao.backCount(map);
        Pages<Order> pages = new Pages<>(count,pageNum,limit);
        List<Order> list = orderDao.backData(map);
        list = list.stream().map(x->{
            x.setStatusName(EnumType.ORDER_STATUS.getName(x.getStatus()));
            if(x.source==2){
                x.setOrder_amount(StringHandler.fenToYuan(StringHandler.getString(x.getOrder_amount())));
                x.setGoods_inner_price(StringHandler.fenToYuan(StringHandler.getString(x.getGoods_inner_price())));
            }
            return x;
        }).collect(Collectors.toList());
        pages.setList(list);
        return pages;
    }

    /**
     * 同意退货
     * @param order_sn 订单编号
     * @param status 变更后订单状态
     * @return
     */
    @Transactional
    public int isBack(String order_sn, int status,String refundAddress) {
        //变更订单状态
        int a = orderDao.editOrderStatus(order_sn,status,refundAddress);
        if(a<=0){
            throw new RuntimeException("变更订单状态失败");
        }
//        //获取订单信息
//        Order detail = orderDao.detail(order_sn);
//        if(Common.isEmpty(detail)){
//            throw new RuntimeException("订单信息不存在");
//        }
        //记录失效表
//        int b = orderDao.addInvalid(order_sn,detail.getMcode(),detail.getMerchant_id(), DateUtil.getDate(new Date(),"yyyy-MM-dd"));
//        if(b<=0){
//            throw new RuntimeException("记录失效表失败");
//        }
//        //分佣明细失效
//        int c = orderDao.expectInvalid(order_sn);
//        if(c<=0){
//            throw new RuntimeException("记录分佣明细失效失败");
//        }
        return 1;
    }

    /**
     * 确认退款
     * @param order_sn 订单编号
     * @param status 变更后订单状态
     * @return
     */
    public int backMoney(String id,String order_sn, int status,int type) {
        if(type==1){
            status = 11;
            int i = editOrderStatus(order_sn,status);
            if(i<1){
                return 0;
            }
        }
        String data = HttpUtils.post("http://172.24.194.56:8082/onlinePay/recharge/refund", MapHandler.initMap("orderId",id));
        JSONObject jsonObject = JSONObject.parseObject(data);
        if(!jsonObject.getString("code").equals("0")){
            throw new RuntimeException(jsonObject.getString("msg"));
        }
        return 0;
    }

}
